<?php
/**
 * description
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     weiyizheng
 * @date       2022/12/7
 * @time       11:17 上午
 */

namespace Buu\Modules\RedisSetting\Controllers;


use App\Models\RedisBusLine;
use App\Models\RedisInstance;
use hhz\constant\basic\businessLine;
use hhz\constant\basic\useScene;
use Illuminate\Support\MessageBag;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;

class RedisBusLineController extends AdminController
{
    public $title = '业务线实例关系';

    public function index(Content $content)
    {
        $tab = new Tab();
        $tab->addLink("Redis Key配置管理", '/admin/RedisSetting/redis-key');
        $tab->add($this->title, $this->grid(), true);
        return $content
            ->title($this->title)
            ->description()
            ->breadcrumb(['text' => '业务线实例关系'])
            ->row($tab);
    }

    public function grid()
    {
        $grid = new Grid(new RedisBusLine());
        $grid->paginate(50);
//        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableBatchActions();
        $grid->disableActions();
        $grid->column('id', 'ID')->sortable();
        $grid->column('bus_name', '名称');
        $grid->column('desc', '描述');
        $grid->column('bus_line', '业务线')->using( businessLine::GROUP)->label();
        $grid->column('scene', '场景')->using(useScene::SCENEGROUP)->label([
            'CACHE' => 'danger',
            'COUNTER' => 'primary',
            'OTHER' => 'default'
        ]);
        $grid->column('r_i_id', 'redis实例')->display(function () {
            return RedisInstance::query()->find($this->r_i_id, ['name'])['name'];
        })->label('warning');
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('bus_line', '业务线')->select(businessLine::GROUP);
                $filter->equal('scene', '场景')->select(useScene::SCENEGROUP);
            });

        });
//        $grid->actions(function (Grid\Displayers\Actions $actions) {
//			$actions->disableDelete();
//        });
        return $grid->render();
    }

    protected function form()
    {
        $form = new Form(new RedisBusLine());
        $form->footer(function (Form\Footer $footer) {
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
            $footer->disableViewCheck();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->text('bus_name', '名称')->placeholder('规则[业务线-场景]')->setWidth(4, 2)->required();
        $form->text('desc', '描述')->setWidth(4, 2)->required();
        $form->select('bus_line', '业务线')->options( businessLine::GROUP)->setWidth(4, 2)->required();
        $form->select('scene', '场景')->options(useScene::SCENEGROUP)->setWidth(4, 2)->required();
        $form->select('r_i_id', 'redis实例')->options(
            RedisInstance::all()->pluck('name', 'id')
        )->setWidth(4, 2)->required();

        // 抛出错误信息
        $form->saving(function ($form) {
            $onlyOne = RedisBusLine::isOnlyOne($form->r_i_id, $form->scene);
            if (!$onlyOne) {
                $error = new MessageBag([
                    'title' => '请注意！！！',
                    'message' => '请保证使用场景与实例的唯一性',
                ]);
                return back()->with(compact('error'))->withInput();
            }
        });

        return $form;
    }
}